package ciyuan.Item.TypeInterface.WeaponType;

import ciyuan.Entity.MagicBall.MagicBallEntity;
import net.minecraft.block.BlockState;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.UseAction;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public abstract class StaffType extends Item implements WeaponType {

    private long lastShootTime=0;
    private long shootInterval=800;

    public StaffType(Properties p_i48487_1_, long shootInterval) {
        super(p_i48487_1_);
        this.shootInterval = shootInterval;
    }

    @Override
    public ActionResult<ItemStack> use(World p_77659_1_, PlayerEntity p_77659_2_, Hand p_77659_3_) {
        p_77659_2_.startUsingItem(p_77659_3_);
        return ActionResult.pass(p_77659_2_.getItemInHand(p_77659_3_));
    }

    @Override
    public void onUseTick(World world, LivingEntity livingEntity, ItemStack itemStack, int p_219972_4_) {
        super.onUseTick(world, livingEntity, itemStack, p_219972_4_);
        if (System.currentTimeMillis()-lastShootTime>shootInterval) {
            if (world.isClientSide()) {
                return;
            }
            lastShootTime = System.currentTimeMillis();
            MagicBallEntity magicBallEntity = new MagicBallEntity(livingEntity,0,0,0,world);
            magicBallEntity.moveTo(livingEntity.getX(),livingEntity.getY()+1,livingEntity.getZ());
            magicBallEntity.shootFromRotation(livingEntity,livingEntity.xRot,livingEntity.yRot,0.0F, 1.5F, 0.0F);
            magicBallEntity.setOwner(livingEntity);
            world.addFreshEntity(magicBallEntity);
            world.playSound(null, livingEntity.getX(),livingEntity.getY(),livingEntity.getZ(), SoundEvents.BLAZE_SHOOT, livingEntity.getSoundSource(), 1.0F, 1.0F);
        }
    }

    @Override
    public boolean canAttackBlock(BlockState p_195938_1_, World p_195938_2_, BlockPos p_195938_3_, PlayerEntity p_195938_4_) {
        return false;
    }

    @Override
    public boolean canBeDepleted() {
        return false;
    }

    @Override
    public int getUseDuration(ItemStack p_77626_1_) {
        return (int)shootInterval;
    }

    @Override
    public boolean hurtEnemy(ItemStack p_77644_1_, LivingEntity p_77644_2_, LivingEntity p_77644_3_) {
        p_77644_1_.hurtAndBreak(1, p_77644_3_, (p_220045_0_) -> {
            p_220045_0_.broadcastBreakEvent(EquipmentSlotType.MAINHAND);
        });
        return true;
    }

    @Override
    public UseAction getUseAnimation(ItemStack p_77661_1_) {
        return UseAction.BOW;
    }
}